// pages/component/navigation_bar/user_bar/user-bar.js
import { 
  USER_RELATION_REVERSE
 } from "../../../request/api";
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    navbarData:{
      type: Object,
      value:{'id': '', 'portrait':'', 'name':'', 'relation':1},
      observer: function(newVal, oldVal){
        if(newVal.fromShare){
          this.setData({
            fromShare: newVal.fromShare,
          });
        }
        var newRelation = newVal.relation || 3;
        this.setData({
          relation: newRelation,
          relationIcon: this._getRelationIconPath(newRelation)
        })
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    height: 0,
    menuLeft: 0, 
    menuBotton: 0,
    statusBarHeight: 0,
    relationPosition: 0,
    relationIcon: '../../../images/btn_follow_normal.png',
    relation: 0,
    fromShare: false,
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () {
      const app = getApp()
      // 定义导航栏的高度,方便对齐
      this.setData({
        statusBarHeight: app.globalData.statusBarHeight,
        height: app.globalData.navBarHeight + app.globalData.menuBotton,
        menuLeft: app.globalData.menuLeft,
        menuBotton: app.globalData.menuBotton,
        relationPosition: app.globalData.menuWidth + app.globalData.menuRight*2
      })
    },
    moved: function () {},
    detached: function () {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  //此处attached的声明会被lifetimes字段中的声明覆盖
  attached: function(){
    const app = getApp()
    // 定义导航栏的高度   方便对齐
    this.setData({
      height: app.globalData.navBarHeight
    })
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 返回上一页面
    _navback() {
      wx.navigateBack();
    },
    //返回到首页
    _backhome() {
      wx.switchTab({
        url: '/pages/index/index',
      });
    },

    _relationReverse: function(){
      USER_RELATION_REVERSE({'id': this.data.navbarData.id}).then((res, reject) =>{
        if(res.data.code == 1){
          wx.showToast({
            title: this.data.relation==2?"关注成功":"取消关注",
          });
          var newRelation = res.data.result.relation;
          var newIconPath = this._getRelationIconPath(newRelation);
          this.setData({
            relation: newRelation,
            relationIcon: newIconPath
          });
        }
      })
    },

    _getRelationIconPath: function(relation){
      var newIconPath = '../../../images/'+(relation == 1? 'btn_following_both_normal' : relation == 2? 'btn_following_normal' : 'btn_follow_normal')+'.png';
      return newIconPath;
    },
  }
})
